/*
 * @Autor: dingxiaolin
 * @Date: 2020-04-25 13:54:09
 * @LastEditors: dingxiaolin
 * @LastEditTime: 2020-04-28 17:37:14
 */
var getRawBody = require('raw-body');
var body = require('body');
const request = require('request-promise')


/*
if you open the initializer feature, please implement the initializer function, as below:
module.exports.initializer = function(context, callback) {
    console.log('initializing');
    callback(null, '');
};
*/
const login = (loginObj) => {
    var options = {
        method: 'POST',
        uri: 'https://test-csp.vip56.cn/csp/User/login',
        body: loginObj,
        json: true // Automatically parses the JSON string in the response
    };

    const reqObj = request(options)
    return reqObj
}

module.exports.handler = function (req, resp, context) {
    getRawBody(req, async (err, body) => {
        console.log('body', body)
        const data = await login(JSON.parse(body.toString()))
        await sendResp(data)
    });

    // 返回
    sendResp = (data) => {
        resp.setHeader('content-type', 'application/json');
        try {
            resp.send(JSON.stringify(data));
        }
        catch (err) {
            resp.send(JSON.stringify({
                success: false,
                data: err.message
            }));
        }
    }
}
